package servidorVehiculo.GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Popup extends JDialog implements ActionListener, WindowListener{

	private static final long serialVersionUID = 1L;

	public final static int MODIFICAR = 0, CREAR = 1, CONECTAR = 2;
	
	private VentanaAdmin ventanaPrincipal;
	private int funcion;
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JTextField tf1;
	private JTextField tf2;
	private JPasswordField tf3;
	private JButton bConfirmar;
	private JButton bVolver;
	
	public Popup(VentanaAdmin v, int funcion, String s1, String s2){
		ventanaPrincipal = v;
		ventanaPrincipal.setEnabled(false);
		this.funcion = funcion;
		initVentana(s1, s2);
		setLocationRelativeTo(ventanaPrincipal);
		setVisible(true);
	}
	
	public Popup(VentanaAdmin v, int funcion){
		ventanaPrincipal = v;
		ventanaPrincipal.setEnabled(false);
		this.funcion = funcion;
		initVentana("","");
		setLocationRelativeTo(ventanaPrincipal);
		setVisible(true);
	}
	
	private void initVentana(String s1, String s2){
		bConfirmar = new JButton("Confirmar");
		bConfirmar.addActionListener(this);
		bVolver = new JButton("Volver");
		bVolver.addActionListener(this);
		
		if(funcion == MODIFICAR){
			label1 = new JLabel("Nombre: ");
			label2 = new JLabel("Clave: ");
			tf1 = new JTextField(12);
			tf2 = new JTextField(12);
			tf1.setText(s1);
			tf2.setText(s2);
			
			setMinimumSize(new Dimension(250, 150));
			setLayout(null);
			setTitle("Modificar usuario");
			setResizable(false);
			addWindowListener(this);

			this.add(label1);
			this.add(label2);
			this.add(tf1);
			this.add(tf2);
			this.add(bConfirmar);
			this.add(bVolver);

			label1.setBounds(
					10, 10, 100, 30);
			tf1.setBounds(100, 10, 90, 25);
			label2.setBounds(10, 40, 100, 30);
			tf2.setBounds(100, 40, 90, 25);
			bConfirmar.setBounds(10, 75, 100, 25);
			bVolver.setBounds(120, 75, 100, 25);
		} else if(funcion == CREAR){
			label1 = new JLabel("Nombre: ");
			label2 = new JLabel("Clave: ");
			tf1 = new JTextField(12);
			tf2 = new JTextField(12);

			setMinimumSize(new Dimension(250, 150));
			setLayout(null);
			setTitle("Crear usuario");
			setResizable(false);
			addWindowListener(this);
			this.add(label1);
			this.add(label2);
			this.add(tf1);
			this.add(tf2);
			this.add(bConfirmar);
			this.add(bVolver);

			label1.setBounds(
					10, 10, 100, 30);
			tf1.setBounds(100, 10, 90, 25);
			label2.setBounds(10, 40, 100, 30);
			tf2.setBounds(100, 40, 90, 25);
			bConfirmar.setBounds(10, 75, 100, 25);
			bVolver.setBounds(120, 75, 100, 25);
		} else if(funcion == CONECTAR){
			
			label1 = new JLabel("IP: ");
			label2 = new JLabel("Usuario: ");
			label3 = new JLabel("Clave: ");
			tf1 = new JTextField(15);
			tf2 = new JTextField(15);
			tf3 = new JPasswordField(15);
			
			setLayout(null);
			setTitle("Conectar");
			setMinimumSize(new Dimension(250, 200));
			setResizable(false);
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);

			this.add(label1);
			this.add(label2);
			this.add(label3);
			this.add(tf1);
			this.add(tf2);
			this.add(tf3);
			this.add(bConfirmar);
			this.add(bVolver);

			label1.setBounds(10, 10, 100, 30);
			tf1.setBounds(100, 10, 90, 25);
			label2.setBounds(10, 40, 100, 30);
			tf2.setBounds(100, 40, 90, 25);
			label3.setBounds(10, 70, 100, 30);
			tf3.setBounds(100,70,90,25);
			bConfirmar.setBounds(10, 105, 100, 25);
			bVolver.setBounds(120, 105, 100, 25);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		if(source == bConfirmar){
			if(funcion == MODIFICAR){
				ventanaPrincipal.modificarUsuario(tf1.getText(), tf2.getText()); 
				ventanaPrincipal.setEnabled(true);
				dispose();
			} else if(funcion == CREAR){
				ventanaPrincipal.crearUsuario(tf1.getText(), tf2.getText()); 
				ventanaPrincipal.setEnabled(true);
				dispose();
			} else if(funcion == CONECTAR){
				ventanaPrincipal.conectar(tf1.getText(), tf2.getText(), new String(tf3.getPassword()));
				ventanaPrincipal.setEnabled(true);
				dispose();
			}
			
		} else if(source == bVolver){
			ventanaPrincipal.setEnabled(true);
			dispose();
		}
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		ventanaPrincipal.setEnabled(true);
		dispose();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
